package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.rsa.Base64Utils;
import framework.common.rsa.RSAUtils;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.AccountModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.anount.LoginActivity;
import hk.m4s.lr.repair.test.ui.anount.RegistActivity;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/29.
 * 修改密码
 */

public class UpdatePassWordActivity extends UeBaseActivity implements View.OnClickListener {
    private EditText old_pass, new_passWord, new_agin_pass;
    private Context context;
    private TextView reg_confirm;
    String oldPass = "";
    String pass = "";
    String aginPass = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_edpassword);
        context = this;
        hiddenFooter();
        showGoBackBtn();
        hiddenNewMessage();
        setTitleText("修改密码");
        initView();
    }

    public void initView() {
        old_pass = (EditText) findViewById(R.id.old_pass);
        new_passWord = (EditText) findViewById(R.id.new_pass);
        new_agin_pass = (EditText) findViewById(R.id.old_agin_pass);
        reg_confirm = (TextView) findViewById(R.id.home_send_order);
        reg_confirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_send_order:
                if (old_pass.getText().toString() == null || old_pass.getText().toString().equals("")) {
                    ToastUtil.toast(context, "请输入旧密码");
                    return;
                } else if (new_passWord.getText().toString() == null || new_passWord.getText().toString().equals("")) {
                    ToastUtil.toast(context, "请输入密码");
                    return;
                } else if (new_agin_pass.getText().toString() == null || new_agin_pass.getText().toString().equals("")) {
                    ToastUtil.toast(context, "请再次输入密码");
                    return;
                } else if (!new_passWord.getText().toString().equals(new_agin_pass.getText().toString())) {
                    ToastUtil.toast(context, "两次密码不一致");
                    return;
                } else {
                    oldPass = old_pass.getText().toString();
                    pass = new_passWord.getText().toString();
                    aginPass = new_agin_pass.getText().toString();
                }
                updatePass();
                break;
        }
    }

    public void updatePass() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("oldPass", oldPass);
            jsondata.put("newPass", pass);
            jsondata.put("repeatPass", aginPass);

            InputStream inPublic = context.getResources().getAssets().open("rsa_public_key.pem");
            PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
            RSAUtils.printPublicKeyInfo(publicKey);
            // 加密
            byte[] encryptByte = RSAUtils.encryptData(jsondata.toString().getBytes(), publicKey);
            // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
            String afterencrypt = Base64Utils.encode(encryptByte);
            parmas.put("jsonText", afterencrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        AccountSerive.updatePass(context, parmas, new ResponseCallback<AccountModel>() {
            @Override
            public void onSuccess(AccountModel accountModel) {
                Intent in=new Intent(UpdatePassWordActivity.this,LoginActivity.class);
                startActivity(in);
                Intent mIntent = new Intent("hk.m4s.lr.repair.test_finish");
                // 发送广播
                sendBroadcast(mIntent);
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}
